package com.zzj.controller;

import com.zzj.base.Result;
import com.zzj.model.dto.mail.MessageDTO;
import com.zzj.service.RedisService;
import com.zzj.utils.Utils;
import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/mail")
public class MailController {

    @Resource
    private JavaMailSender mailSender;
    @Resource
    private RedisService redisService;

    @Transactional
    @PostMapping("sendMessage")
    public Result<?> sendMessage(@RequestBody @Validated MessageDTO in) {
        String to = in.getTo();
        String title = in.getTitle();
        String content = in.getContent();
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("wesley@zzjtpp.top");
        message.setTo(to);
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);
        return Result.success();
    }


    @Transactional
    @PostMapping("code")
    public Result<?> code(@RequestParam("email") String email) {
        try {
            String verificationCode = Utils.getCode(6);
            Result<?> result = redisService.createVerificationCode(email, verificationCode);
            if (result.getCode() != 0) {
                return result;
            }

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("wesley@zzjtpp.top");
            helper.setTo(email);
            helper.setSubject("【Bi8Bo网盘】您的验证码是："+verificationCode);

            // 读取HTML模板文件
            String htmlContent = "<!DOCTYPE html>\n" +
                    "<html>\n" +
                    "<head>\n" +
                    "    <title>【Bi8Bo网盘】</title>\n" +
                    "    <style>\n" +
                    "        body {\n" +
                    "            font-family: Arial, sans-serif;\n" +
                    "            background-color: #f7f7f7;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .container {\n" +
                    "            max-width: 500px;\n" +
                    "            margin: 0 auto;\n" +
                    "            padding: 20px;\n" +
                    "            background-color: #ffffff;\n" +
                    "            border: 1px solid #e0e0e0;\n" +
                    "        }\n" +
                    "        \n" +
                    "        h2 {\n" +
                    "            color: #333333;\n" +
                    "        }\n" +
                    "        \n" +
                    "        p {\n" +
                    "            color: #666666;\n" +
                    "            line-height: 1.5;\n" +
                    "        }\n" +
                    "        \n" +
                    "        strong {\n" +
                    "            color: #009688;\n" +
                    "        }\n" +
                    "    </style>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "    <div class=\"container\">\n" +
                    "        <h2>【Bi8Bo网盘】 用户验证</h2>\n" +
                    "        <p>尊敬的用户：</p>\n" +
                    "        <p>您的验证码是：<strong>{{verificationCode}}</strong></p>\n" +
                    "        <p>请在5分钟内使用该验证码完成验证。</p>\n" +
                    "        <p>感谢您的使用！</p>\n" +
                    "    </div>\n" +
                    "</body>\n" +
                    "</html>";

            // 替换占位符
            htmlContent = htmlContent.replace("{{verificationCode}}", verificationCode);

            // 设置邮件内容为HTML格式
            helper.setText(htmlContent, true);
            mailSender.send(message);
            return Result.success("验证码已发送");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("发送邮件失败");
        }
    }

}
